import {App, Notice} from "obsidian";
import {EditorSelection} from '@codemirror/state';

import {ModalPassword} from 'ModalPassword';
import {CryptoFactory} from 'CryptoFactory';
import {ENCRYPTED_CODE_PREFIX} from 'Constants';

export class UiHelper {
	public async handleDecryptClick(app: App, event: MouseEvent, input: string, secret: string) {
		event.preventDefault();
		const cryptoFactory = new CryptoFactory();
		input = input.replace(ENCRYPTED_CODE_PREFIX, '').replace(/`/g, '').replace(/\s/g, '').replace(/\r?\n|\r/g, '');
		const output = await cryptoFactory.decryptFromBase64(input, secret);
		if (output !== null) {
			await navigator.clipboard.writeText(output);
			new Notice('Secret copied');
		} else {
			new Notice('Secret copy is failed');

		}
	}

	public selectionAndRangeOverlap(selection: EditorSelection, rangeFrom: number, rangeTo: number) {
		for (const range of selection.ranges) {
			if (range.from <= rangeTo && range.to >= rangeFrom) {
				return true;
			}
		}
		return false;
	}

}
